package com.linkivr.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;



public class CSVReader {
	public void read(String csvfile)
	{
		try 
		{
			//String encoding = "UTF-8"; // 字符编码(可解决中文乱码问题 )
			String encoding = "gb2312";
			File file = new File(csvfile);
			if (file.isFile() && file.exists()) 
			{
				InputStreamReader read = new InputStreamReader(
				new FileInputStream(file), encoding);
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTXT = null;
				int cc=0;
				while ((lineTXT = bufferedReader.readLine()) != null) 
				{
					StringTokenizer st = new StringTokenizer(lineTXT, ",");
			    	cc++;
			    	System.out.print(cc);
			    	while (st.hasMoreTokens()) 
			    	  {
			    	  // 每个元素都显示在一个制表符分隔的线
			    		//  st.
			    		//  String ss ;
		                 System.out.print("\t"+st.nextToken() + "\t");
			    	  }
			    	  System.out.println();
					/*
					//将文本读取出来的字符去掉"," 因为读取出来的字符是用","来分隔的
					String text = lineTXT.replaceAll(",", "");
					//用字符分隔成数组
					String[] version_1 = text.split("-");
					for (int i = 0; i < version_1.length; i++) 
					{
						String[] version_2 = version_1[i].split(":");
						for (int j = 0; j < version_2.length; j++) 
						{
								System.out.println(version_2[j]);
						}
						System.out.println("-------");

					}
					*/
				}
				read.close();
			}
			else
			{
				System.out.println("找不到指定的文件！");
			
			} 
		}
		catch (Exception e) {
				System.out.println("读取文件内容操作出错");
				e.printStackTrace();
			

		}


		/*
		try {
		      File csv = new File(csvfile); // CSV数据文件

		      BufferedReader br = new BufferedReader(new FileReader(csv));
		      // 要阅读的最后一行
		      int cc = 0;
		      while (br.ready()) {
		    	  String line = br.readLine();
		    	  // 数据元素分为一行
		    	  StringTokenizer st = new StringTokenizer(line, ",");
		    	  cc++;
		    	  while (st.hasMoreTokens()) 
		    	  {
		    	  // 每个元素都显示在一个制表符分隔的线
		    		//  st.
		    		  String ss=new String( st.nextToken().getBytes( "utf-8"  ),"gb2312" );
	                 System.out.print(cc+"\t"+ss + "\t");
		    	  }
		    	  System.out.println();
		      }
		      br.close();

	    } catch (FileNotFoundException e) {
	      // File对象的创建过程中的异常捕获
	      e.printStackTrace();
	    } catch (IOException e) {
	      // BufferedReader在关闭对象捕捉异常
	      e.printStackTrace();
	    }
	    */
	}

}
